// pages/my/index.js

const app = getApp();
const model = require('../../../utils/model');
var util = require('../../../utils/util');

function formatDate(list){
  for(var i = 0; i < list.length; i ++) {
    list[i]['create_time'] = util.formatTime(new Date(1000*parseInt(list[i]['create_time'])));
  }
}

Page({

  data: {
    list: [],
    scrollHeight: 0,
    lastId:0,
    canLoadMore:true,
    loadtips:'数据加载中...',
    showLoading:false
  },

  getList: function () {
    if (!this.data.canLoadMore){
      return;
    }
    this.data.canLoadMore = false;
    var me = this;
    model.request({
      action:'myReadingList',
      data:{
        last_id:me.data.lastId
      },
      success: function (res) {
        me.data.canLoadMore = true;
  
        if (res.data.list && res.data.list.length) {
          formatDate(res.data.list);
          var newList = me.data.list.concat(res.data.list);
          me.setData({
            list:newList,
            lastId:res.data['last_id'],
            showLoading:false
          });
        }
        else {
          me.setData({
            notShowLoadingFlag:true,
            loadtips:'暂无更多数据'
          });
        }
      }
    });
  },
  
  scroll:function () {
    this.setData({
      showLoading:true
    });
  },

  lower: function () {
    this.getList();
  },

  showReason: function (event) {
    var reason = event.currentTarget.dataset.reason;
    wx.showModal({
        content: reason,
        showCancel: false,
        success: function (res) {
            // if (res.confirm) {
            //     console.log('用户点击确定')
            // }
        }
    });
  },

  publish:function(e){
    var title = e.currentTarget.dataset.title;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/works/publish/publish?title='+title+'&id='+id +'&refer=my_work'
    })
  },

  onLoad: function (options) {
    this.setData({
      scrollHeight: app.globalData.winHeight - 93
    });
    this.getList();

  },

  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {

  },

  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {

  },

  /**
  * 生命周期函数--监听页面隐藏
  */
  onHide: function () {

  },

  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload: function () {

  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {

  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {

  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {

  }
})